<template>
  <div class="page">
    <h2>下载你的代码</h2>
    <el-input
      v-model="fileName"
      placeholder="请输入文件名"
      clearable
      class="input-field"
    />
    <el-button
      type="primary"
      @click="downloadFile"
      :loading="loading"
      class="download-button"
    >
      <span v-if="!loading">下载</span>
      <span v-else>加载中...</span>
    </el-button>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue';

export default defineComponent({
  setup() {
    const fileName = ref('');
    const loading = ref(false);

    const downloadFile = () => {
      if (fileName.value) {
        loading.value = true;
        // 模拟下载功能
        setTimeout(() => {
          const link = document.createElement('a');
          link.href = `/download/${fileName.value}`;
          link.download = fileName.value;
          link.click();
          loading.value = false;
          alert(`下载文件：${fileName.value}`);
        }, 1000); // 模拟下载延迟
      } else {
        alert('请输入文件名');
      }
    };

    return {
      fileName,
      downloadFile,
      loading,
    };
  },
});
</script>

<style scoped>
.page {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 20px;
  background-color: #f5f5f5; /* 背景颜色 */
  border-radius: 8px; /* 圆角效果 */
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* 阴影效果 */
}

h2 {
  margin-bottom: 20px;
  font-size: 1.8rem;
  color: #333; /* 标题颜色 */
}

.input-field {
  width: 100%;
  max-width: 400px;
  margin-bottom: 20px;
}

.download-button {
  width: 100%;
  max-width: 150px;
}
</style>